<?php

require_once 'classes/LightPressPlugin.php';

class AboutContext extends LightPressPlugin {
    
    var $constructor_args = array(
        'separator'         => 'separator',
        'intro'             => 'Text to display before the context data',
        'posts'             => 'text to display the current range of posts being viewed',
        'post'              => 'text to use when there is a single post being displayed',
        'pagination_sep'    => 'separator for the paging indexes',
        'prev'              => 'text for previous range of posts',
        'next'              => 'text for next range of posts',
        'pagination_end'    => 'text to end pagination');
    var $default_context = 6; // LP_CONTEXT_ARCHIVES | LP_CONTEXT_CATEGORY;
    var $description = 'Display the relevant information for archives and categories pages';
    
    
    var $active = true;
    var $hooks = array('post_render');

    var $intro = 'You are viewing';
    var $posts = 'posts %s-%s of %s in';
    var $post = 'the only post in';
    var $separator = 'the';
    var $pagination_sep = ', and';
    var $prev = '%s previous %s';
    var $next = '%s more %s';
    var $pagination_end = 'in this section.';

    var $_messages = array();
            
    
    function AboutContext(&$frontend, $args, $dummy_run=false) {
        
        $this->_frontend =& $frontend;

        foreach ($this->constructor_args as $k=>$v) {
            
            if (isset($args[$k]))
                $this->$k = stripslashes($args[$k]);

            $this->_messages[$k] =& $this->$k;
            
        }
        
    }
    
    function run($hook, &$payload) {
        $tpl =& $this->_frontend->tpl;
        $messages =& $this->_messages;
        $f_messages =& $this->_frontend->messages;
        $context =& $this->_frontend->context;
        
        $tpl->setFile('plugin_aboutcontext', 'plugins/about_context.xml');
        
        $pagination =& $this->_frontend->getPlugin('Pagination');
        if (!$pagination)
            return $this->hide();
        
        $message = '';
        $pagination_message = $pagination_sep = $pagination_end = '';
        $prev_index = $prev_text = $next_text = $next_index = '';
        $prev_posts = $next_posts = '';
        
        $num_posts = $this->_frontend->num_posts;
        $page_next = $pagination->page_next;
        if ($num_posts == 1) {
            $message = $this->_messages['post'];
            $tpl->setBlock('plugin_aboutcontext', 'plugin_aboutcontext_pagination');
            $tpl->setVar('PLUGIN_ABOUTCONTEXT_PAGINATION', '');
        } else {
            
            // build message
            
            $message = sprintf(
                $this->_messages['posts'],
                $pagination->page_current + 1,
                ($page_next >= $num_posts) ? $num_posts : $pagination->page_next,
                $num_posts);

            // build custom pagination
        
            $index = $context['index'];
            $ppp = $this->_frontend->options['posts_per_page'];
            
            if ($index == 0 && $page_next >= $num_posts) {
                // no need to display pagination links
                $tpl->setBlock('plugin_aboutcontext', 'plugin_aboutcontext_pagination');
                $tpl->setVar('PLUGIN_ABOUTCONTEXT_PAGINATION', '');
            } else {
                if ($index == 0) {
                    $tpl->setBlock('plugin_aboutcontext', 'plugin_aboutcontext_prev');
                    $tpl->setVar('PLUGIN_ABOUTCONTEXT_PREV', '');
                } else {
                    $pagination_end = $messages['pagination_end'];
                    $prev_index = $index - $ppp < 0 ? 0 : $index - $ppp;
                    if ($index == 1) {
                        $pagination_message = $f_messages['thereis'];
                        $prev_posts = $f_messages['post'];
                    } else {
                        $pagination_message = $f_messages['thereare'];
                        $prev_posts = $f_messages['posts'];
                    }
                    $prev_text = sprintf($messages['prev'], $pagination->posts_previous, $prev_posts);
                }
            
                if ($page_next >= $num_posts) {
                    $tpl->setBlock('plugin_aboutcontext', 'plugin_aboutcontext_next');
                    $tpl->setVar('PLUGIN_ABOUTCONTEXT_NEXT', '');
                } else {
                
                    $pagination_end = $messages['pagination_end'];
                    $next_index = $index + $ppp;
                
                    if (!empty($prev_text))
                        $pagination_sep = $messages['pagination_sep'];
                    if ($num_posts - $next_index == 1) {
                        $pagination_message = (empty($pagination_message) ? $f_messages['thereis'] : $f_messages['thereare']);
                        $next_posts = $f_messages['post'];
                    } else {
                        $pagination_message = $f_messages['thereare'];
                        $next_posts = $f_messages['posts'];
                    }
                    $next_text = sprintf($messages['next'], $num_posts - ($index + $ppp), $next_posts);
                }
            }
        }
        
        if ($context['type'] == 'category') {
            if ($this->_frontend->raw_urls) {
                $rss_url = str_replace('index.php', 'feed.php', $context['url']);
                if (strpos($rss_url, '?') === false)
                    $atom_url = $rss_url . '?feed=atom';
                else
                    $atom_url = $rss_url . '&amp;feed=atom';
            } else {
                $rss_url = $context['url'] . '/rss2.xml';
                $atom_url = $context['url'] . '/atom.xml';
            }
        } else {
            $tpl->setBlock('plugin_aboutcontext', 'plugin_aboutcontext_catfeed', 'PLUGIN_ABOUTCONTEXT_CATFEED');
            $tpl->setVar('PLUGIN_ABOUTCONTEXT_CATFEED', '');
            $rss_url = '';
            $atom_url = '';
        }
        
        $tpl->setVar(array(
            'plugin_aboutcontext_message'           => sprintf("%s %s %s",
                                                               $messages['intro'],
                                                               $message,
                                                               $messages['separator']),
            'plugin_aboutcontext_title'             => $context['label'],
            'plugin_aboutcontext_context'           => $context['type'],
            'plugin_aboutcontext_pagination_msg'    => $pagination_message,
            'plugin_aboutcontext_pagination_sep'    => $pagination_sep,
            'plugin_aboutcontext_pagination_end'    => $pagination_end,
            'plugin_aboutcontext_rss_url'           => $rss_url,
            'plugin_aboutcontext_atom_url'          => $atom_url,
            'plugin_aboutcontext_prev_index'        => $prev_index,
            'plugin_aboutcontext_prev_text'         => $prev_text,
            'plugin_aboutcontext_prev_posts'        => $prev_posts,
            'plugin_aboutcontext_next_index'        => $next_index,
            'plugin_aboutcontext_next_text'         => $next_text,
            'plugin_aboutcontext_next_posts'        => $next_posts
            ));

        $tpl->parse('PLUGIN_ABOUTCONTEXT', 'plugin_aboutcontext');
    }
    
}

?>